//
//  HuseNetworkTools.swift
//  Huse
//
//  Created by William on 2018/12/13.
//  Copyright © 2018 技术研发-IOS. All rights reserved.
//

import UIKit
import Alamofire

//简写的 key->value
public typealias Parameters = [String : AnyObject]
public typealias SuccessClosure = (AnyObject) -> Void
public typealias FailureClosure = (NSError) -> Void

enum MethodType {
    case get
    case post
    case put
    case delete
}

class HuseNetworkTools: NSObject {
    class var sharedInstance:HuseNetworkTools {
        struct Static {
            static let instance : HuseNetworkTools = HuseNetworkTools()
        }
        return Static.instance
    }
    
    fileprivate override init() {
        super.init()
    }
    
    class func requestData(_ type:MethodType,URLString:String,parameters : Parameters? = nil, finishedBlock:@escaping (_ result: Any) -> ()) {
        let method  = type == .get ? HTTPMethod.get : HTTPMethod.post
        
        let headers: HTTPHeaders = ["key": "", "Key": "", "KEY": ""]
        // 2.发送网络请求
        Alamofire.request(URLString, method: method, parameters: parameters,headers:headers).responseJSON { (response) in
            
            // 3.获取结果
            guard let result = response.result.value else {
                print(response.result.error!)
                return
            }
            
            // 4.将结果回调出去
            finishedBlock(result)
        }
    }
}



